package MOLE.userpkg;

import zyn_hy_login.*;
import DataAccess.*;
import MOLE.*;

public class userx
{

	public static void AutoLogin()
	{

		System.Web.UI.Page p = (System.Web.UI.Page)HttpContext.Current.Handler;
		z8.Common.Users newus = new z8.Common.Users("womenjia");
		p.Session.Add("yh_info", newus);

		p.Session.Add("yh_info", newus);
		p.Session.Add("aspuid", newus.getId());
		p.Session.Add("aspuname", newus.getName());
		p.Session.Add("usercss", "z8_css1.css");
		p.Session["asputreeid"] = newus.getYh_treeid();
		p.Session["aspugroup"] = newus.getYh_group();
		p.Session["aspuTbname"] = newus.getYh_Tbname();
		p.Session["aspuConn"] = newus.getYh_Connstr();

		p.Session["aspuid"] = "xx";

		p.Session.Add("UserId3str", "");




		p.Response.Cookies["userid"].Value = "861109";
		p.Response.Cookies["username"].Value = "zzzzzz";

		p.Response.Cookies["hy_num"].Value = "861109";
		p.Response.Cookies["hy_name"].Value = "zzzzzz";
		p.Response.Cookies["hy_num"].Expires = java.time.LocalDateTime.now().plusDays(70);

		p.Response.Redirect("/z8/z8_user/zyn_manage.aspx");
	}

	public final void login(String uname, String pwd)
	{

		String text = uname;
		if (text.trim().equals(""))
		{
			MOLE.global.showMessage("用户名不能为空");
			return;
		}
		//if (((text.Length <= 6) || (text == "")))
		//{
		//    MOLE.global.showMessage("用户名或密码错误");
		//    return;
		//}
		if (pwd.trim().equals(""))
		{
			MOLE.global.showMessage("密码不能为空");
			return;
		}
		//if (((pwd.Length <= 6) || (text == "")))
		//{
		//    MOLE.global.showMessage("用户名或密码错误");
		//    return;
		//}


		String pToEncrypt = (new SecurityZ8()).Encrypt(pwd);
						DBCommon.strConnectstring = MOLE.GetValue.GetConfigString("ConnectionString");
						SqlConnection connection = new SqlConnection(DBCommon.strConnectstring);
						SqlCommand command = new SqlCommand("select top 1 id,UserName,UserPwd,* from Lot_UserInfo where UserName='" + text + "' and UserPwd='" + pToEncrypt + "'  ", connection);
						connection.Open();
						SqlDataReader reader = command.ExecuteReader();
						if (reader.Read())
						{
							String uid = reader.GetValue(1).toString().trim();
							String usname = reader.GetValue(1).toString().trim();

							System.Web.UI.Page Mypage = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;

							this.loginok = true;
							Mypage.Session["hy_num"] = uid;
							Mypage.Session["hy_name"] = usname;
							//    base.Response.Cookies.Add(new HttpCookie("hy_name", usname));

							//     base.Response.Cookies.Add(new HttpCookie("hy_name", usname));

						}
						else
						{
							MOLE.global.showMessage("用户名或密码错误");
						}




	}


	public final void adduser(String uname, String pwd)
	{

		String text = uname;
		if (text.trim().equals(""))
		{
			MOLE.global.showMessage("用户名不能为空");
			return;
		}
	   if (((text.length() <= 3) || (text.equals(""))))
	   {
		   MOLE.global.showMessage("用户名3位以上");
		  return;
	   }
		if (pwd.trim().equals(""))
		{
			MOLE.global.showMessage("密码不能为空");
			return;
		}
	   if (((pwd.length() < 6)))
	   {
			MOLE.global.showMessage("密码6位以上");
		   return;
	   }


		 WWW.SST.LIB.UserV2 o = new WWW.SST.LIB.UserV2();
			o.uname = uname;
			o.upass = pwd;
			o.email = "";
			o.cp = "";
			o.qq = "";
			//o.qq=qq;
			o.Reg();

			if (o.StausMsg.toLowerCase().equals("ok")) //reg ok
			{
				//labTips.Text=" 注册成功！ ";
				ok = true;
			 //   if (uc.ok)
					MOLE.global.showMessage("添加成功");

			}




	}

	public DataTable dt = new DataTable();

	public final hy_info getuser(String uname)
	{
		hy_info u = new hy_info();
		String s = "select top 1 id,UserName,UserPwd,* from Lot_UserInfo where UserName='" + uname + "' ";
		DataTable d = MOLE.SQL.GetDataTable(s);
		u.setHy_Name(d.Rows[0]["UserName"].toString());
		u.setHy_Num(Integer.parseInt(d.Rows[0]["id"].toString()));
		return u;

	}
	public final hy_info getCurLoginUser()
	{
			 System.Web.UI.Page Mypage = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
			 String uname = Mypage.Session["hy_name"].toString();
			 ;
			 return getuser(uname);

	}

	public static boolean islogin()
	{
		System.Web.UI.Page Mypage = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
		int i = 0;
		try
		{
		 //  
			i = Integer.parseInt(Mypage.Response.Cookies["hy_num"].Value.toString());

			return true;
		}
		catch (RuntimeException e)
		{
			try
			{
				i = Integer.parseInt(Mypage.Session["hy_num"].toString());
				return true;
			}
			catch (RuntimeException e2)
			{
				return false;
			}


		}

	}
	public boolean loginok = false;
	public boolean ok = false;
}